
-- SPDX-License-Identifier: GPL-3.0-or-later
---@class Huanhuaengine: Object
---@field public player_kill table<integer, any>   @ 击杀目标
---@field public player_protect table<integer, any>   @ 保护目标


local Huanhuaengine = class("Huanhuaengine")



function Huanhuaengine:initData(player,data)
    self.layowner = player
    self.room = player.room
    self.player_kill={}
    self.player_protect={}
    --初始化数据
  end

  function Huanhuaengine:setkilltarget(rm)
    local room = rm or self.room
    local pg=table.map(room:getAlivePlayers(), function(p) return p.general end)
    for _, player in ipairs(room.alive_players) do
        local i=player.id
        local targets = table.map(table.filter(room.alive_players, function(p)
            return p.id ~= i and table.contains(pg, p.general) end), function(p) return p end)
        if #targets>0 then
            local target=table.random(targets).general
            table.removeOne(pg,target)
            self.player_kill[i]=target
            player:doNotify("SetPlayerMark", json.encode{i, "@huanhua_kill", tostring(target)})
        else
            local targets = table.map(room:getOtherPlayers(player), function(p) return p end)
            local target=table.random(targets).general
            self.player_kill[i]=target
            player:doNotify("SetPlayerMark", json.encode{i, "@huanhua_kill", tostring(target)})
        
        end
    end

end

function Huanhuaengine:setprotecttarget(rm)
    local room = rm or self.room
    for _, player in ipairs(room.alive_players) do
        local i=player.id
        local targets = table.map(room:getOtherPlayers(player), function(p) return p end)
        local target=table.random(targets).general
        self.player_protect[i]=target
        player:doNotify("SetPlayerMark", json.encode{i, "@huanhua_protect", tostring(target)})

    end

end


--判断是否目标

function Huanhuaengine:istokill(id,who,rm)
    local room = rm or self.room
    if self.player_kill[id]==who.general then
        return true
    end

 

end

function Huanhuaengine:istopro(id,who,rm)
    local room = rm or self.room
    if self.player_protect[id]==who.general then
            return true
    end



end

--奖惩
function Huanhuaengine:rewardkill(who,rm)
    local room = rm or self.room
    for _, player in ipairs(room.alive_players) do
        if self:istokill(player.id,who ,room) then
            room:changeMaxHp(player,1)
            room:recover({
                who = player,
                num = 1,
                recoverBy = player,
              })
            self:rewardSkill(player,who,room)
        end

    end

end

function Huanhuaengine:punishprotect(who,rm)
    local room = rm or self.room
    for _, player in ipairs(room.alive_players) do
        if self:istopro(player.id,who ,room) then
            room:changeMaxHp(player,-1)
            room:loseHp(player,1)
            self:loseSkill(player,room)
        end

    end

end

function Huanhuaengine:loseSkill(who,rm)
    local room = rm or self.room
    local pskills = {}
    for _, skill in ipairs(who.player_skills)  do
        if not string.find(skill.name, "#") then
          table.insert(pskills, skill.name)
        end
      end
      if #pskills>0 then
        local choice = table.random(pskills,1)[1]
        room:handleAddLoseSkills(who, "-"..choice, nil, true, false)
    end
   
end

function Huanhuaengine:rewardSkill(player,who,rm)
    local room = rm or self.room
   
    local pskills = {}
    for _, skill in ipairs(who.player_skills)  do
        if not string.find(skill.name, "#") then
          table.insert(pskills, skill.name)
        end
    end
    if #pskills>0 then
        local choice = table.random(pskills,1)[1]
        room:handleAddLoseSkills(player, choice, nil, true, false)
    end
   
end


function Huanhuaengine:rewardSkills(player,who,rm)
    local room = rm or self.room
   
    
    local pskills = {}
    for _, skill in ipairs(who.player_skills)  do
        if not string.find(skill.name, "#") then
          table.insert(pskills, skill.name)
        end
    end
    if #pskills>0 then
        for _, skill in ipairs(pskills)  do
            room:handleAddLoseSkills(player, skill, nil, true, false)
        end
    end
   
end

Fk:loadTranslationTable{
    ["@huanhua_kill"]="击杀目标",
    ["@huanhua_protect"]="保护目标",
 
  }







  return Huanhuaengine